//
//  ZYAlreadyAgreedController.m
//  BestLearning
//
//  Created by Ethan on 2017/9/26.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ZYAlreadyAgreedController.h"
#import "ZYAlreadyAgreedCell.h"
#import "ZYAlreadyAgreedModel.h"
#define kCell @"cell"
@interface ZYAlreadyAgreedController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *allView;
@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;

@end

@implementation ZYAlreadyAgreedController

- (NSMutableArray *)dataSource {
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的约定";
    self.view.backgroundColor = ColorRGB(247, 245, 245);
    
    
    [self setUpSuperView];
    [self setUpRefresh];
}
#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}






- (void)setNetWork{

    
//    NSInteger roleCode  = [[AccountTool shareAccountTool].currentAccount.rolecode integerValue];
//    NSInteger uid = [[AccountTool shareAccountTool].currentAccount.uid integerValue];
    NSString *url = [NSString stringWithFormat:@"threeGoodElement.do?action=getMyappointList&pageNumber=%ld",(long)self.page];
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    NSLog(@"%@ %@",LLBaseURLs(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        
        [SVProgressHUD dismiss];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            NSDictionary *t = [Obj valueForKey:@"t"];
            NSArray *arr = [t valueForKey:@"dataList"];
            
            for (NSDictionary *dict  in arr) {
                
                ZYAlreadyAgreedModel *model = [ZYAlreadyAgreedModel new];
                model.Id = [dict valueForKey:@"id"];
                
                [model setValuesForKeysWithDictionary:dict];
                
                [self.dataSource addObject:model];
                
            }
            [self.tableView reloadData];
            
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
        }
        
        
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
    
    
   
    
    
    
    
    
}


#pragma mark ----自视图方法的实现
- (void)setUpSuperView{
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth - 20, kScreenHeight - 64)];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 200;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = ColorRGB(247, 245, 245);
    [self.tableView registerClass:[ZYAlreadyAgreedCell  class] forCellReuseIdentifier:kCell];
    
    self.allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 15)];
    self.allView.backgroundColor = ColorRGB(247, 245, 245);
    self.tableView.tableHeaderView = self.allView;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
       self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;   self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    
    
    
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYAlreadyAgreedCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    if (self.dataSource.count > indexPath.row) {
        ZYAlreadyAgreedModel *model = self.dataSource[indexPath.row];
        [cell assignByZYAlreadyAgreedModel:model];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
  //  [cell.exchangeTypeButton addTarget:self action:@selector(exchangeTypeButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    return cell;
}







#pragma mark --- 星币兑换按钮方法的实现
- (void)starId:(NSString *)strId coins:(NSString *)coins{
//    http://192.168.0.113:8080/StarShine/ws/threeGoodElement.do?action=changeAppointStatusCoins&id=22&coins=10
    NSString *url = [NSString stringWithFormat:@"threeGoodElement.do?action=changeAppointStatusCoins&id=%@&coins=%@",strId,coins];
    
    NSLog(@"%@ %@",LLBaseURLs(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            [self setUpRefresh];
        }else{
            [self createAlertViewWithTitle:nil message:[Obj valueForKey:@"msg"] preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
        }
    } Fail:^(NSError *Error) {
        
    }];
    
    
   
    
    
    
}

#pragma mark --- 其他方法的实现
- (void)otherUrlId:(NSString *)userId{
//    http://192.168.0.113:8080/StarShine/ws/threeGoodElement.do?action=changeAppointStatus&id=22
    NSString *url = [NSString stringWithFormat:@"threeGoodElement.do?action=changeAppointStatus&id=%@",userId];
    
    
    
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            [self setUpRefresh];
            
        }else{
            [self createAlertViewWithTitle:nil message:[Obj valueForKey:@"msg"] preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
        }
        
        
    } Fail:^(NSError *Error) {
        
    }];
    
    
    
    
    
    
}








- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
    
}



@end
